﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace SettlersEngine
{
    public class GameSetup
    {
        public ReadOnlyCollection<Guid> Players { get; private set; }
        public Guid Map { get; private set; }
        public TimeSpan PeaceTime { get; set; }

        public GameSetup(Guid inMap, params Guid[] inPlayers)
        {
            Players = new ReadOnlyCollection<Guid>(new List<Guid>(inPlayers));

            Validate();
        }

        public void Validate()
        {

        }
    }

    internal interface IVisualObject : IDisposable
    {
        void Hide();
        void Show();
    }

    internal interface IVisualSizedObject : IVisualObject
    {
        /// <summary>
        /// Represents the total grid width directly derived from animation pixel width.
        /// </summary>
        Int32 VisualWidth { get; }
        /// <summary>
        /// Represents the total grid height directly derived from animation pixel height.
        /// </summary>
        Int32 VisualHeight { get; }
        /// <summary>
        /// Locked bounds determine the grid area that is registered with this object. No other
        /// object can be placed in locked bounds, like buildings or free carriers etc., but
        /// all movable objects are able to free move in this area. Within the locked bounds
        /// there is another area where no movable walk on. 
        /// </summary>
        System.Drawing.Rectangle LockedBounds { get; }
    }

    internal struct CyclePoint
    {
        public Int64 XCycles;
        public Int64 YCycles;

        public Int32 XGrid
        {
            get { return (Int32)(XCycles * RuntimeMap.CYCLES_TO_GRIDPOS); }
            set { XCycles = (Int64)(value / RuntimeMap.CYCLES_TO_GRIDPOS); }
        }

        public Int32 YGrid
        {
            get { return (Int32)(YCycles * RuntimeMap.CYCLES_TO_GRIDPOS); }
            set { YCycles = (Int64)(value / RuntimeMap.CYCLES_TO_GRIDPOS); }
        }

        public Double Y
        {
            get { return YCycles * RuntimeMap.CYCLES_TO_GRIDPOS; }
            set { YCycles = (Int64)Math.Round(value / RuntimeMap.CYCLES_TO_GRIDPOS); }
        }

        public Double X
        {
            get { return XCycles * RuntimeMap.CYCLES_TO_GRIDPOS; }
            set { XCycles = (Int64)Math.Round(value / RuntimeMap.CYCLES_TO_GRIDPOS); }
        }

        public static CyclePoint FromGrid(System.Drawing.Point inPoint)
        {
            CyclePoint result = new CyclePoint();

            result.X = inPoint.X;
            result.Y = inPoint.Y;

            return result;
        }

        public static CyclePoint FromGrid(System.Windows.Point inPoint)
        {
            CyclePoint result = new CyclePoint();

            result.X = inPoint.X;
            result.Y = inPoint.Y;

            return result;
        }

        public static CyclePoint FromGrid(Double inX, Double inY)
        {
            CyclePoint result = new CyclePoint();

            result.X = inX;
            result.Y = inY;

            return result;
        }

        public System.Drawing.Point ToPoint()
        {
            return new System.Drawing.Point(XGrid, YGrid);
        }

        public CyclePoint(Int64 inXCycles, Int64 inYCycles)
        {
            XCycles = inXCycles;
            YCycles = inYCycles;
        }

        public CyclePoint AddCycleVector(System.Drawing.Point inCycleVector)
        {
            return new CyclePoint(XCycles + inCycleVector.X, YCycles + inCycleVector.Y);
        }
    }

    internal abstract class IPositionProvider : IPositionTracker, IDisposable
    {
        public event DChangeHandler<IPositionTracker, System.Drawing.Point> OnTrackedPositionChanged;
        public event DChangeHandler<IPositionTracker, CyclePoint> OnPositionChanged;

        [NonSerialized]
        private IVisualObject m_Visual;
        private CyclePoint m_Position;

        public Object GridNode { get; set; }
        public int TypeIndex { get; private set; }
        public Int64 UniqueID { get; private set; }
        public RuntimeMap Map { get; private set; }
        public virtual IVisualObject UserContext { get { return m_Visual; } set { m_Visual = value; } }

        public System.Drawing.Point GetTrackedPosition()
        {
            return new System.Drawing.Point(Position.XGrid, Position.YGrid);
        }

        public CyclePoint Position { 
            get { return m_Position; }
            set {
                CyclePoint backup = m_Position;
                m_Position = value;

                if (OnPositionChanged != null)
                    OnPositionChanged(this, backup, m_Position);
            }
        }
        public System.Drawing.Point GridPosition { get { return Position.ToPoint(); } }

        public Boolean IsSnappedToGrid { get; set; }

        internal IPositionProvider(RuntimeMap inParent, int inTypeIndex, CyclePoint inInitialPosition)
        {
            TypeIndex = inTypeIndex;
            Map = inParent;
            UniqueID = inParent.AcquireInternalID();
            m_Position = inInitialPosition;
        }

        public void Hide()
        {
            if (UserContext != null)
                UserContext.Hide();
        }

        public void Show()
        {
            if (UserContext != null)
                UserContext.Show();
        }

        public void Dispose()
        {
            if (UserContext != null)
                UserContext.Dispose();

            UserContext = null;
        }
    }

    internal class IStaticObject : IPositionProvider
    {
        /// <summary>
        /// Static objects like stones or wood can provide their consumable resources using a resource provider.
        /// </summary>
        public IResourceObject ResourceProvOrNull { get; internal set; }

        internal IStaticObject(RuntimeMap inParent, int inTypeIndex, CyclePoint inInitialPosition)
            : base(inParent, inTypeIndex, inInitialPosition)
        {
        }
    }


    internal class IMovableObject : IPositionProvider
    {
        private Direction m_Direction;

        public event DChangeHandler<IMovableObject, Direction> OnDirectionChanged;
        internal Object Job { get; set; }
        public MoveableType Type { get; internal set; }
        public Int32 Size { get; private set; }
        public Direction Direction { get { return m_Direction; }
            internal set
            {
                Direction backup = m_Direction;

                m_Direction = value;

                if (OnDirectionChanged != null)
                    OnDirectionChanged(this, backup, Direction);
            }
        }
        internal IMovableState MovableState { get; set; }

        internal IMovableObject(RuntimeMap inParent, MoveableType inType, CyclePoint inInitialPosition)
            : base(inParent, (int)inType, inInitialPosition)
        {
            Type = inType;
            Size = 1;
        }

        public void Stop()
        {
            MovableState.Stop();
        }
    }

    internal enum GroundType : byte
    {
        Water,
        Mountain,
        Grass,
        Swamp,
        Snow,
    }

    [Flags]
    internal enum WallType : byte
    {
        None = 0,
        Building = 1,
        AutoMovable = 2,
        UserMovable = 4,
        Nature = 8,
        AreaLock = 16,
    }
}
